package clientcore;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;

public class ConnectServer {
    private Socket gameClient = null;
    private InetAddress serverAddr = null;
    private String hostname = null;
    private int serverPort = 0;
    private AndroidMain ac = null;
    private DataOutputStream sendServer = null;
    private DataInputStream receiveServer = null;


    public ConnectServer(String hostname, int serverPort,AndroidMain ac) {
        this.hostname = hostname;
        this.serverPort = serverPort;
        this.ac = ac;
        connectServer(hostname, serverPort);

    }

    private void connectServer(String hostname, int serverPort) {
        try {
            serverAddr = InetAddress.getByName(hostname);
            gameClient = new Socket(serverAddr,serverPort);
            }catch(UnknownHostException ex) {
                ac.log("Unknown Host Exception in class "+this.getClass().toString()+" and method connectServer()");
            }catch(IOException ex) {
                ac.log("IOException in "+this.getClass().toString()+" and method connectServer()");
            }

    }

    public DataOutputStream getDataOutputStream() {
        try {
            sendServer = new DataOutputStream(gameClient.getOutputStream());

        } catch (IOException ex) {
            ac.log("IOException in "+this.getClass().toString()+" and method getDataOutputStream()");
        }
        return sendServer;
    }

    public DataInputStream getDataInputStream() {
        try {
            receiveServer = new DataInputStream(gameClient.getInputStream());
        } catch (IOException ex) {
            ac.log("IOException in "+this.getClass().toString()+" and method getDataInputStream()");
        }
        return receiveServer;
    }






}
